package com.vavr.checkincms.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;

import com.vavr.checkincms.common.domain.BaseResult;
import com.vavr.checkincms.common.domain.ResultEnum;
import com.vavr.checkincms.domain.dto.CheckinUserDTO;
import com.vavr.checkincms.service.WechatLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 单点登录
 *
 * @since 2.0.0
 */
@Slf4j
@RestController
public class WechatLoginController {

    /**
     * meetingParticipantService
     */
    private final WechatLoginService wechatLoginService;

    public WechatLoginController(WechatLoginService wechatLoginService) {
        this.wechatLoginService = wechatLoginService;
    }

    /**
     * 微信授权 & 生成本地用户信息
     *
     * @return 登录结果
     */
    @GetMapping("/sso/login/code")
    public BaseResult<CheckinUserDTO> loginByCode(@RequestParam("code") @NotBlank(message = "code不能为空") String code,
            @RequestParam("meetingId") @NotBlank(message = "meetingId不能为空") String meetingId,
            HttpServletRequest request, HttpServletResponse response) {
        return BaseResult.of(ResultEnum.OK, wechatLoginService.loginWechat(request, response, code, meetingId));
    }

    /**
     * 微信授权 & 生成本地用户信息 测试方法
     *
     * @return 登录结果
     */
    @GetMapping("/httpsweb/*")
    public BaseResult<CheckinUserDTO> loginByCodeTest(
            @RequestParam("code") @NotBlank(message = "code不能为空") String code, HttpServletRequest request,
            HttpServletResponse response) {
        String meetingId = "ldlpx2024";
        return BaseResult.of(ResultEnum.OK, wechatLoginService.loginWechat(request, response, code, meetingId));
    }
}
